<!--微信账号-->
<template>
  <div class="app-container">
    <div class="search">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="true" label-width="68px">
      <el-input
        class="search-input"
        v-model="queryParams.wxName"
        placeholder="账号名称"></el-input>

      <el-button type="primary" icon="el-icon-search" size="mini" @click="getPlayerWxTransactions" style="margin-left: 20px;">筛选</el-button>
      <el-button type="primary" size="mini" @click="getReviewTransactions">待审核</el-button>
      <el-button type="primary" size="mini" @click="getAllTransactions">全部</el-button>
      </el-form>
      <el-button type="primary" icon="el-icon-plus" @click="goClick(0)">申请充值</el-button>
    </div>
    <h1>充值账单</h1>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        label="账号名称"
        align="center"
        prop="wxName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="交易类型"
        align="center"
        prop="transactionType"
        :show-overflow-tooltip="true"
        :formatter="transactionTypeFormatter"
      />
      <el-table-column
        label="金额"
        align="center"
        prop="amount"
        :show-overflow-tooltip="true"
        :formatter="formatAmount"
      />
      <el-table-column
        label="余额"
        align="center"
        prop="balance"
        :show-overflow-tooltip="true"
        :formatter="formatAmount"
      />
      <el-table-column
        label="交易申请时间"
        align="center"
        prop="transactionDisplayTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center" >
       <template slot="header" scope="scope">操作</template>
        <template slot-scope="scope">
          <!-- 根据 status 值显示不同的内容 -->
          <div v-if="scope.row.status === 1">
            已审核通过
          </div>
          <div v-if="scope.row.status === -1" style="color: gray; text-decoration: line-through;">
            已取消充值
          </div>
          <div v-else-if="scope.row.status === 0">
            <el-button
              size="mini"
              type="text"
              @click="goClick(1, scope.row)">审核通过</el-button>
            <el-button
              size="mini"
              type="text"
              style="color: red;"
              @click="goClick(2, scope.row)">取消充值</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getTransactions"
    />
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px">
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="账号">
          <el-select
            style="width: 100%"
            v-model="form.playerWx"
            filterable
            remote
            reserve-keyword
            placeholder="查询账号"
            :remote-method="getPlayerWx"
            :loading="selectloading"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.wxId"
              :label="item.wxId + ' ' + item.wxName"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model.number="form.amount" clearable></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="5"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  searchTransactions,
  requestRecharge,
  confirmTransaction,
  rejectTransaction,
} from "@/api/data/transaction.js";
import {
  goSearchWeChat,
} from "@/api/account/wx.js";
export default {
  data() {
    return {
      queryParams: {
        wxName: undefined,
        type:1,
        status: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      title: "申请充值",
      loading: false,
      tableData: [],
      total: 0,
      dialogVisible: false,
      form: {
        playerWx: undefined,
        amount: 0.00,
        remark: "",
      },
      rules: {
        playerWx: [ { required: true, message: "请选择账号", trigger: "blur" },  ],
        amount: [ { required: true, message: "请输入金额", trigger: "blur" }, ],
      },
      selectloading: false,
      options: [],
    };
  },
  mounted() {
    this.getAllTransactions();
  },
  methods: {
    // 查询微信账号列表
    getAllTransactions() {
      this.queryParams.wxName = undefined;
      this.queryParams.status = undefined;

      this.getTransactions();
    },

    getPlayerWxTransactions(){
      this.queryParams.status = undefined;
      this.getTransactions();
    },

    getReviewTransactions(){
       this.queryParams.status = 0;
       this.getTransactions();
    },

    getTransactions() {
      this.loading = true;
      searchTransactions(this.queryParams).then((res) => {
        console.info("searchTransactions info:", this.tableData);
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      }).catch((error) => {
        // 处理接口异常
        console.error("searchTransactions error:", error);
        this.loading = false;
      });
    },

    transactionTypeFormatter(row, column) {
      const transactionType = row[column.property];
      if (transactionType == 1) {
        return '充值';
      }
      if (transactionType == 2) {
        return '提现';
      }
      return '未知' ;
    },

    // 操作
    goClick(type, row) {
      switch (type) {
        case 0:
          this.dialogVisible = true;
          break;
        case 1:
          this.form = { ...row };
          confirmTransaction(this.form).then((res) => {
            console.info("confirmTransaction info:", this.tableData);
            // -> 刷新表单
            this.getTransactions();
          }).catch((error) => {
            // 处理接口异常
            console.error("confirmTransaction error:", error);
          });
          break;
        case 2:
          this.form = { ...row };
          let info = `取消 ${this.form.wxName} 的 $${this.form.amount.toFixed(2)} 充值`;
          console.info("goClick 取消 :", info);
          this.$alert(info, '是否取消操作', {
              confirmButtonText: '确定',
              callback: action => {
                // 用户确认后的回调, 取消充值
                rejectTransaction(this.form).then((res) => {
                    console.info("rejectTransaction success:");
                    // -> 刷新表单
                    this.getTransactions();
                 }).catch((error) => {
                    // 处理接口异常
                    console.error("rejectTransaction error:", error);
                 });
              }
            });
          break;
      }
    },
    // 申请上分
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.info("submitForm playerWx1 :", this.form.playerWx);
          let params = {
             playerWxId: this.form.playerWx.id,
             wxName: this.form.playerWx.wxName,
             amount: this.form.amount,
             transactionType: 1,
             status: 0,
             remark: this.form.remark,
          };
          console.info("submitForm-> requestRecharge :", params);
          requestRecharge(params).then((res) => {
                  console.info("requestRecharge success:");
                  this.dialogVisible = false;
                  // -> 刷新表单
                  this.getTransactions();
                }).catch((error) => {
                  // 处理接口异常
                  console.error("requestRecharge error:", error);
                });
        } else {
          return false;
        }
      });
    },


    // 重置表单
    reset() {
      this.form = {
        wxId: "",
        wxName: "",
        refereeWxId: "",
        insurerWxId: "",
        remark: "",
      };
      this.resetForm("form");
    },
    // 跳转微信详情
    goWxInfo(row) {
      this.$router.push({
        path: "/player/wxDetail",
        query: { id: row.id },
      });
    },
    // 模糊查询保险人/推荐人
    getPlayerWx(query) {
      if (query !== "") {
        this.selectloading = true;
        goSearchWeChat({ key: query }).then((res) => {
          this.options = res.rows;
          this.selectloading = false;
        }).catch((error) => {
           // 处理接口异常
           console.error("getPlayerWx :", query, error);
           this.selectloading = false;
        });
      } else {
        this.options = [];
      }
    },
    formatAmount(row, column, cellValue) {
        return cellValue.toFixed(2);
    },
  },
};
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  .search-input {
    width: 240px;
  }
}
</style>
